from django.apps import apps
import os

import django

from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'HopeIESystem.settings')

django.setup()

# 获取ReportManager应用的配置对象

report_manager_config = apps.get_app_config('ReportManager')

# 获取ReportManager应用中所有模型类

models = report_manager_config.get_models()

# 根据这些模型名在当前创建python文件 如果已经存在则跳过  文件名格式为 model_name_views.py
for model in models:
    # 获取每个模型类的名称,名称区分大小写
    model_name = model.__name__

    # 创建文件
    file_name = f'{model_name}_Views.py'
    # 如果当面目录有这个文件则跳过否则创建文件
    if not os.path.exists(file_name):
        # 创建一个空文件
        with open(file_name, 'w') as f:

            f.write(f"""from django.db.models.expressions import RawSQL
from django.utils.dateparse import parse_datetime
from rest_framework import status
from rest_framework.parsers import MultiPartParser, FormParser
from rest_framework.response import Response
from rest_framework.views import APIView

from ReportManager.models import {model_name} as Report
from ReportManager.serializers import {model_name}Serializer as Serializer""")



    else:
        print(f'{file_name}文件已经存在')
